package com.member.shop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.member.common.utils.DateUtils;
import com.member.shop.domain.SysBizGoods;
import com.member.shop.domain.SysBizGoodsSku;
import com.member.shop.mapper.SysBizGoodsSkuMapper;
import com.member.shop.service.ISysBizGoodsSkuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * 商品skuService业务层处理
 *
 * @author lixinchen
 * @date 2024-01-05
 */
@Service
public class SysBizGoodsSkuServiceImpl extends ServiceImpl<SysBizGoodsSkuMapper, SysBizGoodsSku> implements ISysBizGoodsSkuService {
    @Resource
    private SysBizGoodsSkuMapper sysBizGoodsSkuMapper;

    /**
     * 查询商品sku
     *
     * @param id 商品sku主键
     * @return 商品sku
     */
    @Override
    public SysBizGoodsSku selectSysBizGoodsSkuById(Long id) {
        return sysBizGoodsSkuMapper.selectById(id);
    }

    /**
     * 查询商品sku列表
     *
     * @param sysBizGoodsSku 商品sku
     * @return 商品sku
     */
    @Override
    public List<SysBizGoodsSku> selectSysBizGoodsSkuList(LambdaQueryWrapper<SysBizGoodsSku> queryWrapper) {
        return sysBizGoodsSkuMapper.selectList(queryWrapper);
    }

    /**
     * 新增商品sku
     *
     * @param sysBizGoodsSku 商品sku
     * @return 结果
     */
    @Override
    public int insertSysBizGoodsSku(SysBizGoodsSku sysBizGoodsSku) {
        sysBizGoodsSku.setCreateTime(DateUtils.getNowDate());
        return sysBizGoodsSkuMapper.insert(sysBizGoodsSku);
    }

    /**
     * 修改商品sku
     *
     * @param sysBizGoodsSku 商品sku
     * @return 结果
     */
    @Override
    public int updateSysBizGoodsSku(SysBizGoodsSku sysBizGoodsSku) {
        sysBizGoodsSku.setUpdateTime(DateUtils.getNowDate());
        return sysBizGoodsSkuMapper.updateById(sysBizGoodsSku);
    }

    /**
     * 批量删除商品sku
     *
     * @param ids 需要删除的商品sku主键
     * @return 结果
     */
    @Override
    public int deleteSysBizGoodsSkuByIds(Long[] ids) {
        return sysBizGoodsSkuMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除商品sku信息
     *
     * @param id 商品sku主键
     * @return 结果
     */
    @Override
    public int deleteSysBizGoodsSkuById(Long id) {
        return sysBizGoodsSkuMapper.deleteById(id);
    }

    @Override
    public SysBizGoodsSku selectSysBizGoodsSkuByGoodsIdAndKey(Long goodsId, String specsKey) {
        SysBizGoodsSku sysBizGoodsSku = new SysBizGoodsSku();
        sysBizGoodsSku.setGoodsId(goodsId);
        sysBizGoodsSku.setSpecsKey(specsKey);
        List<SysBizGoodsSku> sysBizGoodsSkus = sysBizGoodsSkuMapper.selectSysBizGoodsSkuList(sysBizGoodsSku);

        if (sysBizGoodsSkus == null || sysBizGoodsSkus.isEmpty()) {
            return null;
        }
        return sysBizGoodsSkus.get(0);
    }


    /**
     * 删除商品sku信息
     *
     * @param goodsId 商品主键
     * @return 结果
     */
    @Override
    public int deleteSysBizGoodsSkuByGoodsId(Long goodsId) {
        return sysBizGoodsSkuMapper.deleteSysBizGoodsSkuById(goodsId);
    }

    @Override
    public int lockStocks(Long id, Long number) {
        return sysBizGoodsSkuMapper.lockStocks(id, number);
    }

    @Override
    public void releaseStocks(Long goodsId, String specsKey, Long number) {
        sysBizGoodsSkuMapper.releaseStocks(goodsId, specsKey, number);
    }

    @Override
    public List<SysBizGoodsSku> selectSkuList(SysBizGoods sysBizGoods) {
        return  sysBizGoodsSkuMapper.selectSkuList(sysBizGoods);
    }
}
